package gu.simplemq.mqtt;

import java.util.Map;
import com.google.common.net.HostAndPort;

import gu.simplemq.BaseMessageQueueFactory;
import gu.simplemq.IConsumer;
import gu.simplemq.IProducer;
import gu.simplemq.IPublisher;
import gu.simplemq.ISubscriber;
import gu.simplemq.MQProperties;
import gu.simplemq.MessageQueueType;

import static gu.simplemq.mqtt.MqttPoolLazys.createInstance;
import static gu.simplemq.mqtt.PropertiesHelper.MHELPER;

/**
 * {@link IMessageQueueFactory}接口的MQTT(不完整)实现类
 * @author guyadong
 *
 */
public final class MessageQueueFactoryImpl extends BaseMessageQueueFactory<MqttPoolLazy> {

	private volatile MQProperties props;
	public MessageQueueFactoryImpl() {
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	protected void doInit(Map<String, Object> properties){
		mqConnParams = (Map)properties;
		props = MHELPER.initParameters(properties);
		pool = createInstance(props);
	}
	@Override
	protected HostAndPort doGetHostAndPort() {
		return MHELPER.getHostAndPort(props);
	}

	@Override
	public ISubscriber getSubscriber() {
		return MqttFactory.getSubscriber(getPool());
	}

	@Override
	public IPublisher getPublisher() {
		return MqttFactory.getPublisher(getPool());
	}

	@Override
	public IProducer getProducer() {
		throw new UnsupportedOperationException();
	}

	@Override
	public IConsumer getConsumer() {
		throw new UnsupportedOperationException();
	}

	@Override
	public final MessageQueueType getImplType(){
		throw new UnsupportedOperationException();
	}

	@Override
	public boolean testConnect(){
		return MHELPER.testConnect(props,null);
	}
}
